package cn.com.zh.app.collector.controller;

import java.io.File;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.com.zh.app.common.util.ResultVO;

@RestController
public class FileUploadController {

	/**
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/uploadFile")
	public ResultVO<Object> uploadFile(HttpServletRequest request,HttpServletResponse response) {
		ResultVO<Object> vo=new ResultVO<Object>();
		try {
//			CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext());
				MultipartHttpServletRequest multipartHttpServletRequest=(MultipartHttpServletRequest) request;
				Iterator<?> iterator=multipartHttpServletRequest.getFileNames();
				while(iterator.hasNext()) {
					MultipartFile multipartFile=multipartHttpServletRequest.getFile(iterator.next().toString());
					if(multipartFile !=null) {
						String path="E://files//"+multipartFile.getOriginalFilename();
						multipartFile.transferTo(new File(path));
						vo.setCode("000000");
						vo.setMessage("success");
					}
				}
			
			
		}catch(Exception e) {
			vo.setCode("999999");
			vo.setMessage("fail");
			vo.setBody(e.getStackTrace());
			e.printStackTrace();
		}

		return vo;
	}
}
